angular.module('baisonWaitUpLoad', ['baisonDb', 'baisonService'])
        //获取未上传信息
        .factory('waitUpLoad', ['$q', '$rootScope', '$location', 'dbSelect', 'apiService', 'dbUpdate', 'panDian', 'ReceiptRecord', 'VIP', function ($q, $rootScope, $location, dbSelect, apiService, dbUpdate, panDian, ReceiptRecord, VIP) {
                var receipt_record_get_wait_upload_group_list = function (callback) {
                    return ReceiptRecord.get_list_local_not_upload();
                };
                var vip_get_wait_upload_group_list = function (callback) {
                    return VIP.get_list_local_not_upload();

                };
                var take_stock_get_wait_upload_group_list = function (callback) {
                    return panDian.get_list_local_not_upload();
                };
                //获取小票明细
                var receipt_detail_by_record_code_local = function (record_code, callback) {
                    var sql = 'select * from PosReceiptDetail where record_code="' + record_code + '"';
                    return dbSelect('YsPos', sql);

                };
                //获取结算方式
                var receipt_settlement_by_record_code_local = function (record_code, callback) {
                    var sql = 'select * from PosReceiptSettlement where record_code="' + record_code + '"';
                    dbSelect('YsPos', sql).then(function (ret) {
                        callback(ret);
                    }).catch(function (err) {
                        baison_app.api.alert({msg: err});
                    });
                };
                var receipt_promotion_by_record_code_local = function(record_code,callback){
                    var sql = 'select * from PosReceiptPromotion where record_code="' + record_code + '"';
                    dbSelect('YsPos', sql).then(function (ret) {
                        callback(ret);
                    }).catch(function (err) {
                        baison_app.api.alert({msg: err});
                    });
                };
                //上传离线小票
                var receipt_record_loacl_to_api = function (record_data) {
                    var deferred = {};
                    var promise = {};
                    var record_list = record_data;
                    var offline = false;
                    $.each(record_list, function (i, n) {
                        deferred[i] = $q.defer();
                        promise[i] = deferred[i].promise;
                        receipt_detail_by_record_code_local(n.record_code).then(function (retlocaldeail) {
                            //console.log(retlocaldeail);
                            retlocaldeail.forEach(function(n,i){
                                retlocaldeail[i].guide_user_code = n.daogou_code;
                            });
                            var details = retlocaldeail;
                            var record = n;
                            record.is_add_person = n.trade_user_code;
                            record.sell_time = record.record_time;
                            var data = {};
                            data.record = record;
                            data.detail = details;
                            receipt_promotion_by_record_code_local(n.record_code,function(retpromotion){
                                //console.log(retpromotion);
                                data.promotion=retpromotion;    
                                apiService('pos/pos/add', data).then(function (retserveradd) {
                                    //console.log(retserveradd);
                                    
                                    if (retserveradd.data.status == 1) {
                                        receipt_settlement_by_record_code_local(n.record_code, function (retlocal) {
                                            //console.log(retlocal);
                                            var settlement = retlocal;
                                            var _settlement_data = {};
                                            _settlement_data.record_code = record.record_code;
                                            _settlement_data.payment = settlement;
                                            _settlement_data.record = record;
                                            _settlement_data.device_sn = baison_app.api.device_sn;
                                            var _record=[];
                                            _record[0]=record;
                                            _record[0].total_money =_record[0].final_money ;
                                        
                                            _settlement_data.record_data={
                                                'record':_record,
                                                'record_detail':details,
                                                'payment':settlement,
                                                'promotion':retpromotion
                                            };
                                            //console.log(_settlement_data);
                                            apiService('pos/pos/update', _settlement_data).then(function (retupdate) {
                                                //console.log(retupdate);
                                                if (retupdate.data.status == 1) {

                                                    dbUpdate('PosReceiptRecord', {'comment': retupdate.data.message, 'is_upload': 1}, {'record_code': record.record_code}, function (ret) {
                                                        //baison_app.api.alert({msg:ret,title:'localret'});
                                                    });

                                                    deferred[i].resolve('success');

                                                } else {
                                                    dbUpdate('PosReceiptRecord', {'comment': 'fail:' + retupdate.data.message}, {'record_code': record.record_code}, function (ret) {
                                                    });
                                                    deferred[i].resolve('success');

                                                }

                                            });
                                        });
                                    } else {

                                        dbUpdate('PosReceiptRecord', {'comment': 'fail:' + retserveradd.data.message}, {'record_code': record.record_code}, function (ret) {
                                        });

                                        deferred[i].resolve('success');

                                    }
                                }).catch(function (err) {

                                    if (!offline) {
                                        offline = true;
                                        baison_app.api.alert({title: $rootScope.language.COMMON_TITLE_HINT, msg: $rootScope.language.MSG_NETWORK_ABNORMAL});
                                        baison_app.api.hideProgress();
                                    }
                                });   
                            });
                        

                        });
                    });

                    return $q.all(promise);


                };
                //上传离线会员
                var vip_loacl_to_api = function (vip_data) {

                    var deferred = {};
                    var promise = {};
                    var offline = false;
                    var record_list = vip_data;
                    $.each(record_list, function (i, n) {
                        deferred[i] = $q.defer();
                        promise[i] = deferred[i].promise;
                        var data = {
                            'customer_code': n.customer_code,
                            'customer_name': n.customer_name,
                            'customer_tel': n.customer_tel,
                            'vip_code': n.vip_code,
//                            customer_level: '001',
//                    shop_code:
                            'customer_sex': n.customer_sex,
                            'email': n.email,
                            'address': (n.address == "undefined") ? '' : n.address,
//                    work_address:
                            'birthday': n.birthday,
                            'shop_code':$rootScope.current_shop.ShopCode,
                            'user_code':$rootScope.user.UserCode,
                            'VIP_category':(n.VIP_category == "undefined")? 0 :n.VIP_category,
//                        customer_integral: '0',
//                        consume_money: '0',
//                        consume_num: '0',
//                    password:
//                    id_card:
//                    status:
//                    nickname:
//                    type:
//                    level_time:
//                    is_online:
//                    is_offline:
//                    country:'1'
                            'province': (n.province == "undefined") ? '' : n.province,
                            'city': (n.city == "undefined") ? '' : n.city,
                            'district': (n.county == "undefined") ? '' : n.county,
//                    marriage:
//                    education:
                            'skin_type': n.skin,
                            'weixin_id': n.weixin,
//                    is_edit:
//                    base_tel:
//                    base_home_tel:
                            'qq': (n.qq == "undefined") ? '' : n.qq,
                            'daogouyuan': n.is_add_person
//                        is_add_time: n.is_add_time
//                        is_upload: '0'
                        }
//                        var data = n;
//                        data.push(cus)
                        //baison_app.api.alert({msg:data,title:'datatatata'});
                        //console.log(data);
                        apiService('crm/vip/add_customer', data).then(function (ret) {
                            //console.log(ret);
                            if (ret.data.status == 1) {
                                //baison_app.api.alert({msg: ret.data.message});
                                dbUpdate('crm_customer', {'is_upload': 1, 'comment': ret.data.message}, {'customer_code': n.customer_code}, function (ret) {

                                });
                                deferred[i].resolve('success');
                            } else {
                                dbUpdate('crm_customer', {'comment': 'fail:' + ret.data.message}, {'customer_code': n.customer_code}, function (ret) {

                                });
                                deferred[i].resolve('success');
                            }

                        }).catch(function (err) {
                            if (!offline) {
                                offline = true;
                                baison_app.api.alert({title: $rootScope.language.COMMON_TITLE_HINT, msg: $rootScope.language.MSG_NETWORK_ABNORMAL});
                                baison_app.api.hideProgress();
                            }
                        });

                    });
                    return $q.all(promise);

                };
                return {
                    receipt_record_get_wait_upload_group_list: receipt_record_get_wait_upload_group_list,
                    vip_get_wait_upload_group_list: vip_get_wait_upload_group_list,
                    take_stock_get_wait_upload_group_list: take_stock_get_wait_upload_group_list,
                    receipt_record_loacl_to_api: receipt_record_loacl_to_api,
                    vip_loacl_to_api: vip_loacl_to_api
                };
            }]);